<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Goods;
use app\model\MiaoshaGoods;

class MiaoshagoodsController extends Base
{
    public function index()
    {
        $uniacid = uniacid();
            $page = input('post.page', 1, 'intval');
            $keyword = input('post.keyword', '', 'serach_in');
            $ocid = input('post.ocid', '', 'serach_in');

            if (empty($ocid)) {
                $ocid = $this->ocid;
            }

            $query = MiaoshaGoods::where(['uniacid' => $uniacid, 'status' => 1]);

            $priceOrder = input('post.priceOrder');
            $salesOrder = input('post.salesOrder');

            if (!empty($priceOrder)) {
                $Sort = 'ms_price ' . $priceOrder;
            } elseif (!empty($salesOrder)) {
                $Sort = 'sale_count ' . $salesOrder;
            } else {
                $Sort = 'sort asc,id desc';
            }

            if (!empty($ocid)) {
                $query->where('ocid', $ocid);
            }

            if (!empty($keyword)) {
                $query->where('title', 'like', '%' . $keyword . '%');
            }

            $res = $query->order($Sort)
                ->paginate(getpage())
                ->toArray();

            foreach ($res['data'] as &$vo) {
                $vo['create_time'] = time_format($vo['create_time']);
                $vo['update_time'] = time_format($vo['update_time']);
            }
            $res['data'] = MiaoshaGoods::setGoodslist($res['data']);

            $data['data'] = $res;

        return $this->json($data);
    }
}
